﻿Imports System.Data.OleDb
Public Class Albums
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Assign text to the heading
        myHeading.InnerHtml = "Albums"

        'Check if member is a "Swimmer" to disable to add album button (only available to tourists and greece lovers)
        If (Roles.IsUserInRole(User.Identity.Name, "Swimmer")) Then
            btn_Galleries.Enabled = False
            lbl_MaxAlbums.Text = "You cannot add albums yet!"
        End If
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim AlbumsCountSql As String = "SELECT * FROM TotalAlbumsPerTourist WHERE Tourist = @f1"
        Dim cmdAlbumsCount As OleDbCommand = New OleDbCommand(AlbumsCountSql, oleDbConn)
        cmdAlbumsCount.CommandType = CommandType.Text
        cmdAlbumsCount.Parameters.AddWithValue("@f1", User.Identity.Name)
        oleDbConn.Open()
        Dim drAlbumsCount As OleDbDataReader = cmdAlbumsCount.ExecuteReader()
        If drAlbumsCount.HasRows = True Then
            drAlbumsCount.Read()
            'Check if member is a "Tourist" and has already added 1 album
            If (Roles.IsUserInRole(User.Identity.Name, "Tourist")) Then
                If (drAlbumsCount("TotalAlbums") = 1) Then
                    btn_Galleries.Enabled = False
                    lbl_MaxAlbums.Text = "Tourist's can add only one album!"
                End If
            End If
            'Check if member is a "Greece Lover" and has already added 5 albums
            If (Roles.IsUserInRole(User.Identity.Name, "Greece Lover")) Then
                If (drAlbumsCount("TotalAlbums") = 5) Then
                    btn_Galleries.Enabled = False
                    lbl_MaxAlbums.Text = "Maximum number of albums reached!"
                End If
            End If
        End If
        If IsPostBack = False Then
            'Count Activity for page
            Dim SqlStringActivity As String = "INSERT INTO SiteActivity(Activity) VALUES (@f1)"
            Dim cmdActivity As OleDbCommand = New OleDbCommand(SqlStringActivity, oleDbConn)
            cmdActivity.CommandType = CommandType.Text
            cmdActivity.Parameters.AddWithValue("@f1", "Albums")
            cmdActivity.ExecuteNonQuery()
            oleDbConn.Close()
        End If
    End Sub

    Protected Sub btn_findByName_Click(sender As Object, e As EventArgs) Handles btn_findByName.Click
        searchDatabase("AlbumName", tb_search.Text)
    End Sub

    Protected Sub btn_findByRegion_Click(sender As Object, e As EventArgs) Handles btn_findByRegion.Click
        searchDatabase("Tourist", tb_search.Text)
    End Sub

    Private Function searchDatabase(ByVal buttonStr As String, ByVal searchStr As String)
        SqlDataSource2.SelectCommand = "SELECT * FROM TotalVotesPerAlbum WHERE " & buttonStr & " LIKE '%" & searchStr & "%'"
        SqlDataSource2.DataBind()
        Return True
    End Function

    Protected Sub SqlDataSource2_Selected(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource2.Selected
        'If datasource is empty
        If (e.AffectedRows < 1) Then
            myHeading.InnerHtml = "No Album found"
        End If
        If (e.AffectedRows < 9) Then
            DataPagerBeaches.Visible = False
        Else
            DataPagerBeaches.Visible = True
        End If
    End Sub

    Protected Sub ListView1_DataBound(sender As Object, e As EventArgs)
        'Display datapager only if there is more than one page to display
        DataPagerBeaches.Visible = (DataPagerBeaches.PageSize < DataPagerBeaches.TotalRowCount)
    End Sub

End Class